package com.xinxin.leetcode.problem661;

import com.xinxin.leetcode.common.CommonUtil;

import java.util.Arrays;

import static com.xinxin.leetcode.common.CommonUtil.sout;

/**
 * @author ShiXinXin
 * @date 2020-02-16 23:30
 */
class Solution {
    public int[][] imageSmoother(int[][] M) {
        int[][] arr = new int[M.length][M[0].length];
        for (int i = 0; i < M.length; i++) {
            for (int j = 0; j < M[i].length; j++) {
                int num = 0;
                for (int m = i - 1; m <= i + 1; m++) {
                    if (m < 0 || m >= M.length) {
                        continue;
                    }
                    for (int n = j - 1; n <= j + 1; n++) {
                        if (n >= 0 && n < M[i].length) {
                            arr[i][j] += M[m][n];
                            num++;
                        }
                    }
                }
                arr[i][j] /= num;
            }
        }
        return arr;
    }

    public static void main(String[] args) {
        int[][] arr = {
                {1, 1, 1},
                {1, 0, 1},
                {1, 1, 1}
        };
        System.out.println((Arrays.deepToString(new Solution().imageSmoother(arr))));
    }
}
